package com.zlx.sharelive.activity;

import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.zlx.module_base.base_ac.BaseAc;
import com.zlx.module_base.base_api.res_data.FriendListBean;
import com.zlx.module_base.base_api.util.ApiUtil;
import com.zlx.module_base.base_util.SPUtil;
import com.zlx.module_network.api1.livedata.BaseObserver;
import com.zlx.module_network.api1.livedata.BaseObserverCallBack;
import com.zlx.module_network.bean.ApiResponse;
import com.zlx.sharelive.R;
import com.zlx.sharelive.adapter.CommonRecyclerViewAdapter;
import com.zlx.sharelive.adapter.CommonRecyclerViewHolder;
import com.zlx.sharelive.utils.GlideImgManager;
import com.zlx.sharelive.utils.StringUt;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 我的伴侣
 */
public class MyCompanionAc extends BaseAc {

    @BindView(R.id.rv_data)
    RecyclerView rv_data;

    private List<FriendListBean.InfoDTO.RecordsDTO> mList = new ArrayList<>();
    private CommonRecyclerViewAdapter<FriendListBean.InfoDTO.RecordsDTO> mAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.ac_my_companion;
    }

    @Override
    public void initViews() {
        super.initViews();

        getFriendList();
        initRecyclerView();
    }

    /**
     * 我的伴侣列表
     */
    private void getFriendList() {
        ApiUtil.getUserApi().getFriendList(SPUtil.getToken(this)).observe(this, new BaseObserver<>(new BaseObserverCallBack<ApiResponse<FriendListBean>>() {
            @Override
            public void onSuccess(ApiResponse<FriendListBean> data) {
                mList.clear();
                if (null != data) {
                    if (null != data.getData()) {
                        if (null != data.getData().getInfo()) {
                            if (data.getData().getInfo().size() > 0) {
                                if (null != data.getData().getInfo().get(0).getRecords()) {
                                    mList.addAll(data.getData().getInfo().get(0).getRecords());
                                }
                            }
                        }
                    }
                }
                mAdapter.notifyDataSetChanged();
            }
        }));
    }

    private void initRecyclerView() {
        rv_data.setLayoutManager(new LinearLayoutManager(this));

        mAdapter = new CommonRecyclerViewAdapter<FriendListBean.InfoDTO.RecordsDTO>(this, mList) {
            @Override
            public void convert(CommonRecyclerViewHolder holder, FriendListBean.InfoDTO.RecordsDTO entity, int position) {
                if (!StringUt.isEmpty(entity.getAvatar())) {
                    GlideImgManager.glideLoader(MyCompanionAc.this, entity.getAvatar(), holder.getView(R.id.iv_recommend_head), 0);
                }
                holder.setText(R.id.tv_recommend_nickname, entity.getNickName());
            }

            @Override
            public int getLayoutViewId(int viewType) {
                return R.layout.item_my_companion;
            }
        };

        rv_data.setAdapter(mAdapter);

        mAdapter.setOnRecyclerViewItemClickListener(new CommonRecyclerViewAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View v, int position) {
                FriendListBean.InfoDTO.RecordsDTO data = mList.get(position);
                Intent intent = new Intent();
                intent.putExtra("data", data);
                setResult(30, intent);
                finish();
            }
        });
    }
}
